#!/usr/bin/env bash

source ./config/env.sh
clear

mkdir -p ${K8S_CA_DIR}/scheduler

cat > ${K8S_CA_DIR}/scheduler/kube-scheduler-csr.json <<EOF
{
    "CN": "system:kube-scheduler",
    "hosts": [
      "127.0.0.1",
    ],
    "key": {
        "algo": "rsa",
        "size": 2048
    },
    "names": [
      {
        "C": "CN",
        "ST": "BeiJing",
        "L": "BeiJing",
        "O": "system:kube-scheduler",
        "OU": "4Paradigm"
      }
    ]
}
EOF

sed -i '5 i\ \ \ \ \ \ "'`echo ${MASTER_IP} |  cut -d " " -f 1`'"' ${K8S_CA_DIR}/scheduler/kube-scheduler-csr.json
for ((i=2; $i<=${MASTER}; i++))
do
  sed -i '5 i\ \ \ \ \ \ "'`echo ${MASTER_IP} |  cut -d " " -f $i`'",' ${K8S_CA_DIR}/scheduler/kube-scheduler-csr.json
done

cd ${K8S_CA_DIR}/scheduler

cfssl gencert -ca=${K8S_CA_DIR}/admin/ca.pem \
  -ca-key=${K8S_CA_DIR}/admin/ca-key.pem \
  -config=${K8S_CA_DIR}/admin/ca-config.json \
  -profile=kubernetes kube-scheduler-csr.json | cfssljson -bare kube-scheduler

for master_ip in $MASTER_IP
do
  echo "=========================================== ${master_ip} ==========================================="
  ssh root@${master_ip} "test -e ${K8S_CA_DIR}/scheduler || mkdir -p ${K8S_CA_DIR}/scheduler"
  scp ${K8S_CA_DIR}/scheduler/kube-scheduler* root@${master_ip}:${K8S_CA_DIR}/scheduler
done

${K8S_BIN_DIR}/kubectl config set-cluster kubernetes \
  --certificate-authority=${K8S_CA_DIR}/admin/ca.pem \
  --embed-certs=true \
  --server=${APISERVER} \
  --kubeconfig=kube-scheduler.kubeconfig

${K8S_BIN_DIR}/kubectl config set-credentials system:kube-scheduler \
  --client-certificate=${K8S_CA_DIR}/scheduler/kube-scheduler.pem \
  --client-key=${K8S_CA_DIR}/scheduler/kube-scheduler-key.pem \
  --embed-certs=true \
  --kubeconfig=kube-scheduler.kubeconfig

${K8S_BIN_DIR}/kubectl config set-context system:kube-scheduler \
  --cluster=kubernetes \
  --user=system:kube-scheduler \
  --kubeconfig=kube-scheduler.kubeconfig

${K8S_BIN_DIR}/kubectl config use-context system:kube-scheduler --kubeconfig=kube-scheduler.kubeconfig


for master_ip in ${MASTER_IP}
do
  echo "=========================================== ${master_ip} ==========================================="
  scp ${K8S_CA_DIR}/scheduler/kube-scheduler.kubeconfig root@${master_ip}:${K8S_CA_DIR}/scheduler
done



mkdir -p ${K8S_DATA}/config/scheduler/yaml

cat > ${K8S_DATA}/config/scheduler/yaml/kube-scheduler.yaml << EOF
apiVersion: kubescheduler.config.k8s.io/v1alpha1
kind: KubeSchedulerConfiguration
clientConnection:
  kubeconfig: "${K8S_CA_DIR}/scheduler/kube-scheduler.kubeconfig"
leaderElection:
  leaderElect: true
EOF

for master_ip in ${MASTER_IP}
do
  echo "=========================================== ${master_ip} ==========================================="
  ssh root@${master_ip} "test -e ${K8S_DATA}/config/scheduler/yaml || mkdir -p ${K8S_DATA}/config/scheduler/yaml"
  scp ${K8S_DATA}/config/scheduler/yaml/kube-scheduler.yaml root@${master_ip}:${K8S_DATA}/config/scheduler/yaml
done

cat > ${K8S_DATA}/config/scheduler/kube-scheduler.service <<EOF
[Unit]
Description=Kubernetes Scheduler
Documentation=https://github.com/GoogleCloudPlatform/kubernetes

[Service]
WorkingDirectory=${K8S_DATA}/data/scheduler
ExecStart=${K8S_BIN_DIR}/kube-scheduler \\
  --config=${K8S_DATA}/config/scheduler/yaml/kube-scheduler.yaml \\
  --address=127.0.0.1 \\
  --kube-api-qps=100 \\
  --logtostderr=true \\
  --v=2
Restart=always
RestartSec=5
StartLimitInterval=0

[Install]
WantedBy=multi-user.target
EOF

for master_ip in ${MASTER_IP}
do
  echo "=========================================== ${master_ip} ==========================================="
  scp ${K8S_DATA}/config/scheduler/kube-scheduler.service root@${master_ip}:/etc/systemd/system/
done

for master_ip in ${MASTER_IP}
do
  echo "=========================================== ${master_ip} ==========================================="
  ssh root@${master_ip} "test -e ${K8S_DATA}/data/scheduler || mkdir -p ${K8S_DATA}/data/scheduler"
  ssh root@${master_ip} "systemctl daemon-reload && systemctl enable kube-scheduler && systemctl restart kube-scheduler"
done

sleep 10
for master_ip in ${MASTER_IP}
do
  echo "=========================================== ${master_ip} ==========================================="
  ssh root@${master_ip} "systemctl status kube-scheduler|grep Active"
done

${K8S_BIN_DIR}/kubectl get endpoints kube-scheduler --namespace=kube-system  -o yaml